package com.nf.health.app.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.nf.health.app.utils.PreferenceHelper;

/**
 * Created by Jason on 2016/3/11.
 * 负责数据库表的创建和升级
 */
public class DbOpenHelper extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION = 2;
    private static final String USERNAME_TABLE_CREATE = "CREATE TABLE "
            + UserDao.TABLE_NAME + " ("
            + UserDao.COLUMN_NAME_NICK + " TEXT, "
            + UserDao.COLUMN_NAME_AVATAR + " TEXT, "
            + UserDao.COLUMN_NAME_ID + " TEXT PRIMARY KEY);";
    private static final String PATIENT_TABLE_CREATE = "CREATE TABLE "
            + PatientDao.TABLE_NAME + " ("
            + "_id INTEGER PRIMARY KEY AUTOINCREMENT, "
            + PatientDao.COLUMN_NAME_NICK + " TEXT, "
            + PatientDao.COLUMN_NAME_GENDER + " INTEGER, "
            + PatientDao.COLUMN_NAME_CHECKED + " INTEGER, "
            + PatientDao.COLUMN_NAME_BIRTHDAY + " TEXT);";
    private static final String QUESTION_TABLE_CREATE = "CREATE TABLE "
            + QuestionDao.TABLE_NAME + " ("
            + "_id INTEGER PRIMARY KEY AUTOINCREMENT, "
            + QuestionDao.COLUMN_QUESTION_ID + " TEXT, "
            + QuestionDao.COLUMN_USRE_DESCRIPTION + " TEXT, "
            + QuestionDao.COLUMN_QUESTION_CONTENT + " TEXT, "
            + QuestionDao.COLUMN_IMAGES + " TEXT);";

    private static final String CHAT_TIME_TABLE_CREATE = "CREATE TABLE "
            + ChatTimeDao.TABLE_NAME + " ("
            + ChatTimeDao.COLUMN_USER_ID + " TEXT PRIMARY KEY,"
            + ChatTimeDao.COLUMN_TIME + " TEXT);";

    private static final String GROUP_TABLE_CREATE = "CREATE TABLE "
            + GroupInfoDao.TABLE_NAME + " ("
            + GroupInfoDao.COLUMN_NAME_ID + " TEXT PRIMARY KEY,"
            + GroupInfoDao.IS_FORBIT_TALK + " TEXT);";
    private static final String GROUPMANAGER_TABLE_CREATE = "CREATE TABLE "
            + GroupManagerInfoDao.TABLE_NAME + " ("
            + GroupManagerInfoDao.COLUMN_NAME_ID + " TEXT PRIMARY KEY,"
            + GroupManagerInfoDao.name + " TEXT, "
            + GroupManagerInfoDao.nickName + " TEXT);";

    private static DbOpenHelper instance = null;

    private DbOpenHelper(Context context) {
        super(context, getUserDatabaseName(), null, DATABASE_VERSION);
    }

    public static DbOpenHelper getInstance(Context context) {
        if (instance == null) {
            instance = new DbOpenHelper(context.getApplicationContext());
        }
        return instance;
    }

    private static String getUserDatabaseName() {
        //TODO 数据库名字
        String dbName = PreferenceHelper.getString(PreferenceHelper.USERNAME, "temp") + ".db";
        return dbName;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(USERNAME_TABLE_CREATE);
        db.execSQL(PATIENT_TABLE_CREATE);
        db.execSQL(QUESTION_TABLE_CREATE);
        db.execSQL(CHAT_TIME_TABLE_CREATE);
        db.execSQL(GROUP_TABLE_CREATE);
        db.execSQL(GROUPMANAGER_TABLE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        /*
        if(oldVersion < 2){
            db.execSQL("ALTER TABLE "+ UserDao.TABLE_NAME +" ADD COLUMN "+
                    UserDao.COLUMN_NAME_AVATAR + " TEXT ;");
        }*/
        if (oldVersion < 2) {
            db.execSQL(USERNAME_TABLE_CREATE);
            db.execSQL(PATIENT_TABLE_CREATE);
            db.execSQL(QUESTION_TABLE_CREATE);
            db.execSQL(CHAT_TIME_TABLE_CREATE);
            db.execSQL(GROUP_TABLE_CREATE);
            db.execSQL(GROUPMANAGER_TABLE_CREATE);
            db.execSQL("DROP TABLE IF EXISTS user;");
            db.execSQL("DROP TABLE IF EXISTS bracelet;");
        }
    }

    public void closeDB() {
        if (instance != null) {
            try {
                SQLiteDatabase db = instance.getWritableDatabase();
                db.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            instance = null;
        }
    }
}
